package board.command;

import java.awt.*;
import java.util.*;

import sgf.SGFParseException;

import board.Board;

/**
 * Represents a Command that can be performed on the Board.
 * @author TKington
 *
 */
public abstract class Command {
	/**
	 * Performs the Command.
	 * @param b the Board to operate on
	 * @return true if successful
	 */
    public abstract boolean doIt(Board b);
    
    /**
     * Undoes the Command.
     * @param b the Board to operate on
     */
    public abstract void undoIt(Board b);
    
    /**
     * Gets the bounding Rectangle of the area affected by the Command.
     * @return the bounding Rectangle
     */
    public abstract Rectangle getBounds();
    
    /**
     * Checks if the Command is legal on a Board of a given size.
     * @param size the size
     * @throws SGFParseException if the Command is illegal
     */
    public abstract void validatePoints(int size) throws SGFParseException;
    
    /**
     * Creates a Command object from an SGF tag and a list of properties.
     * @param tag the SGF tag
     * @param props the properties
     * @return the new Command object
     * @throws SGFParseException if the properties are illegal
     */
    public static Command createCommand(String tag, ArrayList props) throws SGFParseException {
        if(tag.equals("B")) //$NON-NLS-1$
            return new MoveCommand(1, props);
        if(tag.equals("W")) //$NON-NLS-1$
            return new MoveCommand(-1, props);
        if(tag.equals("AB")) //$NON-NLS-1$
            return new AddCommand(1, props);
        if(tag.equals("AW")) //$NON-NLS-1$
            return new AddCommand(-1, props);
        //System.out.println("Unrecognized tag: " + tag);
        return null;
    }
}
